/**
 *	@file	album_main.c
 *	@brief	フォトアルバムメイン
 *	@author	Miyuki Iwasawa
 *	@date	08.05.20
 */

#include "common.h"
#include "system/procsys.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/snd_tool.h"
#include "system/fontproc.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/buflen.h"
#include "system/window.h"
#include "system/brightness.h"
#include "system/clact_util.h"
#include "system/clact_tool.h"
#include "system/render_oam.h"
#include "system/palanm.h"
#include "system/wipe.h"
#include "system/window.h"
#include "system/font_arc.h"
#include "system/winframe.naix"
#include "gflib/touchpanel.h"
#include "poketool/pokeicon.h"
#include "poketool/monsno.h"
#include "msgdata/msg.naix"
#include "communication/communication.h"

#include "savedata/config.h"
#include "application/album_app.h"
#include "album_sys.h"
#include "album_sub.h"




//=========================================================
//プロトタイプ
//=========================================================
static void album_ViewClear(void);
static void album_InitData(ALBUM_WORK* wk);
static void album_EndData(ALBUM_WORK* wk);

static BOOL app_Init(ALBUM_WORK* wk);
static BOOL app_End(ALBUM_WORK* wk);
static int seq_FadeIn(ALBUM_WORK* wk);
static int seq_FadeOut(ALBUM_WORK* wk);
static int seq_PicSelect(ALBUM_WORK* wk);
static int seq_PageUp(ALBUM_WORK* wk);
static int seq_PageDown(ALBUM_WORK* wk);
static int seq_ListSelect(ALBUM_WORK* wk);
static int seq_PicMove(ALBUM_WORK* wk);
static int seq_MoveEffect(ALBUM_WORK* wk);
static int seq_PicDel(ALBUM_WORK* wk);



//=========================================================
//プロセス定義
//=========================================================
/**
 *	@brief	アルバムケース呼び出し
 */
PROC_RESULT AlbumProc_Init(PROC* proc,int* seq)
{

	switch(*seq){
	case 0:
		album_ViewClear();

		//ヒープ作成
		HeapStatePush();
		sys_CreateHeap(HEAPID_BASE_APP,HEAPID_ALBUM_APP,0x20000);

		{
			ALBUM_WORK* wk = NULL;
			wk = PROC_AllocWork(proc,sizeof(ALBUM_WORK),HEAPID_ALBUM_APP);
			MI_CpuClear8(wk,sizeof(ALBUM_WORK));

			wk->heapID = HEAPID_ALBUM_APP;
			wk->param = (ALBUM_PARAM*)PROC_GetParentWork(proc);

			//引継ぎデータ初期化
			album_InitData(wk);

		}
		(*seq)++;
		break;
	case 1:
		if(app_Init((ALBUM_WORK*)PROC_GetWork(proc))){
			return PROC_RES_FINISH;
		}
	}
	return PROC_RES_CONTINUE;
}

/**
 *	@brief	アルバムケースメイン
 */
PROC_RESULT AlbumProc_Main(PROC* proc,int* seq)
{
	ALBUM_WORK* wk = (ALBUM_WORK*)PROC_GetWork(proc);

	switch(*seq){
	case SEQ_FADE_IN:
		*seq = seq_FadeIn(wk);
		break;
	case SEQ_PIC_SELECT:
		*seq = seq_PicSelect(wk);
		break;
	case SEQ_PAGE_UP:
		*seq = seq_PageUp(wk);
		break;
	case SEQ_PAGE_DOWN:
		*seq = seq_PageDown(wk);
		break;
	case SEQ_LIST_SELECT:
		*seq = seq_ListSelect(wk);
		break;
	case SEQ_PIC_MOVE:
		*seq = seq_PicMove(wk);
		break;
	case SEQ_MOVE_EFFECT:
		*seq = seq_MoveEffect(wk);
		break;
	case SEQ_PIC_DEL:
		*seq = seq_PicDel(wk);
		break;
	case SEQ_FADE_OUT:
		*seq = seq_FadeOut(wk);
		break;
	case SEQ_EXIT:
		return PROC_RES_FINISH;
	}
	CATS_Draw(wk->pActRes);
	return PROC_RES_CONTINUE;
}

/**
 *	@brief	アルバムケース終了
 */
PROC_RESULT AlbumProc_End(PROC* proc,int* seq)
{
	ALBUM_WORK* wk = (ALBUM_WORK*)PROC_GetWork(proc);

	if(!app_End(wk)){
		return PROC_RES_CONTINUE;
	}
	album_ViewClear();

	//引継ぎデータ書き戻し
	album_EndData(wk);

	//ワーク解放
	PROC_FreeWork(proc);

	HeapStatePop();
	HeapStateCheck(HEAPID_ALBUM_APP);

	sys_DeleteHeap(HEAPID_ALBUM_APP);

	return PROC_RES_FINISH;
}

///////////////////////////////////////////////////////////////////////////////
//


/**
 *	@brief	画面初期化
 */
static void album_ViewClear(void)
{
	sys_VBlankFuncChange(NULL, NULL);
	sys_HBlankIntrStop();	//HBlank割り込み停止

	GF_Disp_GX_VisibleControlInit();
	GF_Disp_GXS_VisibleControlInit();
	GX_SetVisiblePlane(0);
	GXS_SetVisiblePlane(0);

	WIPE_SetBrightness(WIPE_DISP_MAIN,WIPE_FADE_BLACK);
	WIPE_SetBrightness(WIPE_DISP_SUB,WIPE_FADE_BLACK);
	WIPE_ResetWndMask(WIPE_DISP_MAIN);
	WIPE_ResetWndMask(WIPE_DISP_SUB);
}

/**
 *	@brief	引継ぎデータ初期化
 */
static void album_InitData(ALBUM_WORK* wk)
{
	CONFIG* config;

	//キーモード初期化
	wk->key_mode = KeyTouchStatus_CheckTouchOrKey(wk->param->kt_status);

	//コンフィグ
	config = SaveData_GetConfig(wk->param->save);
	wk->msg_spd = CONFIG_GetMsgPrintSpeed(config);
	wk->win_type = CONFIG_GetWindowType(config);

	//初期選択データ設定
	{
		u8 pos;

		pos = Didx2Vpos(wk->param->ret_idx % PAGE_DATA_NUM);
		wk->page = wk->param->ret_idx / PAGE_DATA_NUM;
		wk->cpos_x = pos % PAGE_DNUM_X;
		wk->cpos_y = pos / PAGE_DNUM_X;
	}

	//写真データ
	wk->pict_save = SaveData_GetPictureSave(wk->param->save);
	wk->pictData = PictureSave_AllocPictureDataArray(wk->pict_save,wk->heapID);
	wk->pict_num_ini = PictureSave_PictureDataNum(wk->pict_save);
	wk->pict_num = wk->pict_num_ini;

	wk->page_max = (wk->pict_num/PAGE_DATA_NUM);
	if(wk->pict_num%PAGE_DATA_NUM){
		++wk->page_max;
	}
	//写真数が0のとき、上記だとページ最大が0になってしまうので、１にする
	//ページ送りの動作条件がpage_max-1のため	bts4567	nagihashi
	if( wk->pict_num==0)
	{
		wk->page_max	= 1;
	}
	AlbumSub_PictureListInit(wk);
}

/**
 *	@brief	引継ぎデータ終了処理
 */
static void album_EndData(ALBUM_WORK* wk)
{
	//写真データ書き戻し
	AlbumSub_PictureListSave(wk);

	sys_FreeMemoryEz(wk->pictData);

	//キーモード書き戻し
	KeyTouchStatus_SetTouchOrKey(wk->param->kt_status,wk->key_mode);

}

/**
 *	@brief	初期化
 *
 */
static BOOL app_Init(ALBUM_WORK* wk)
{
	switch(wk->seq){
	case 0:
		AlbumResInit(wk);
		++wk->seq;
		break;
	case 1:
		AlbumSub_InitialDraw(wk);

		//タッチパネル初期化
		InitTPSystem();
		InitTPNoBuff(4);

		wk->seq = 0;
		return TRUE;
	}
	return FALSE;
}

/**
 *	@brief	終了処理
 *
 */
static BOOL app_End(ALBUM_WORK* wk)
{
	//タッチパネル終了
	StopTP();

	AlbumResRelease(wk);
	return TRUE;
}

/**
 *	@brief	フェードイン
 *
 */
static int seq_FadeIn(ALBUM_WORK* wk)
{
	switch(wk->seq){
	case 0:
		WIPE_SYS_Start(
			WIPE_PATTERN_WMS, WIPE_TYPE_FADEIN,
			WIPE_TYPE_FADEIN, WIPE_FADE_BLACK, WIPE_DEF_DIV, WIPE_DEF_SYNC, wk->heapID);
		wk->seq++;
		break;
	case 1:
		if( !WIPE_SYS_EndCheck()){
			break;
		}
		wk->seq = 0;
		return SEQ_PIC_SELECT;
	}
	return SEQ_FADE_IN;
}

/**
 *	@brief	フェードアウト
 *
 */
static int seq_FadeOut(ALBUM_WORK* wk)
{
	switch(wk->seq){
	case 0:
		WIPE_SYS_Start(
			WIPE_PATTERN_WMS, WIPE_TYPE_FADEOUT,
			WIPE_TYPE_FADEOUT, WIPE_FADE_BLACK, WIPE_DEF_DIV, WIPE_DEF_SYNC, wk->heapID);
		wk->seq++;
		break;
	case 1:
		if( !WIPE_SYS_EndCheck()){
			break;
		}
		wk->seq = 0;
		return SEQ_EXIT;
	}
	return SEQ_FADE_OUT;
}


/**
 *	@brief	メイン
 *
 */
static int seq_PicSelect(ALBUM_WORK* wk)
{
	return AlbumSub_InputSelect(wk);
}

/**
 *	@brief	ページめくり　一つ前へ
 */
static int seq_PageUp(ALBUM_WORK* wk)
{
	if(!AlbumPageUp(wk)){
		return SEQ_PAGE_UP;
	}
	if(wk->state == STATE_MOVE){
		return SEQ_PIC_MOVE;
	}else{
		return SEQ_PIC_SELECT;
	}
}

/**
 *	@brief	ページめくり　一つ後ろへ
 */
static int seq_PageDown(ALBUM_WORK* wk)
{
	if(!AlbumPageDown(wk)){
		return SEQ_PAGE_DOWN;
	}
	if(wk->state == STATE_MOVE){
		return SEQ_PIC_MOVE;
	}else{
		return SEQ_PIC_SELECT;
	}
}

/*
 *	@brief	リスト選択
 */
static int seq_ListSelect(ALBUM_WORK* wk)
{
	return AlbumListSelect(wk);
}

/*
 *	@brief	写真移動
 */
static int seq_PicMove(ALBUM_WORK* wk)
{
	return AlbumSub_InputMove(wk);
}

/**
 *	@brief	写真移動エフェクト
 */
static int seq_MoveEffect(ALBUM_WORK* wk)
{
	if(!AlbumMoveEffect(wk)){
		return SEQ_MOVE_EFFECT;
	}
	return SEQ_PIC_SELECT;
}

/*
 *	@brief	リスト選択
 */
static int seq_PicDel(ALBUM_WORK* wk)
{
	return AlbumPictureDel(wk);
}

